<?php
// +----------------------------------------------------------------------
// | Created by Sublime: [ Conqweal ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~20223[chenjia] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.99dn.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <150672834@qq.com>
// +----------------------------------------------------------------------
// | Date: 2021/12/22 0026
// +----------------------------------------------------------------------
// | Description:管理员管理
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\model\Admin as amodel;
use app\admin\model\User as cmodel;
use support\Request;
use think\facade\Db;
use app\BackendController;

class AdminsController extends BackendController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    function list() {
        $roles        = Db::name('role')->column('r_name', 'r_id');
        $default_role = '';
        return view("admins/list", compact('roles', 'default_role'));
    }

    public function listData(Request $request)
    {
        $limit = $request->get("limit", 10);
        $q     = cmodel::alias("u")->join("user_profile p", " u.u_id=p.u_id", "left")->join('admin a', "a.u_id=u.u_id", 'inner');
        if ($request->get('role')) {
            $q->join("role_user ru", "ru.u_id=u.u_id", "left")->where("ru.r_id", $request->get('role'));
        }
        $request->get('id') && $q->where('u.u_id', $request->get('id'));
        $request->get('username') && $q->where('u.username', 'like', '%' . $request->get('username') . '%');
        $request->get('email') && $q->where('u.email', 'like', '%' . $request->get('email') . '%');
        $request->get('phone') && $q->where('u.phone', 'like', '%' . $request->get('phone') . '%');
        $request->get('sex') && $q->where('u.sex', $request->get('sex'));
        $ls = $q->field("u.*,a.id as admin_id,p.truename")->paginate(['list_rows' => $limit, 'request' => request()->all()]);
        $q->order("a.id", "desc");

        foreach ($ls as $k => $v) {
            $r_names = [];
            foreach ($v->roles as $v2) {
                $r_names[] = $v2->role->r_name;
            }
            $v['r_names']   = implode(',', $r_names);
            $v->sex_text    = $v->sex_text;
            $v->status_text = $v->status_text;
            $ls[$k]         = $v;
        }

        return json(['code' => $ls->total() > 0 ? 0 : 201, 'msg' => '无数据', 'count' => $ls->total(), 'data' => $ls->items()]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view("admins/create");
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $u_id   = $request->post('u_id');
        $amodel = new amodel();
        $msg    = '保存成功';
        $status = 1;
        try {
            $amodel->save(['u_id' => $u_id]);
        } catch (\Exception $e) {
            $status = -1;
            $msg    = '发生错误';
        }
        return json(['status' => $status, 'msg' => $msg]);
    }

    public function delete(Request $request)
    {
        $id = $request->input("id");
        amodel::where('id', 'in', $id)->delete();
        $data = ['msg' => '删除成功', 'status' => 1];
        return json($data);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

}
